var sys = require('util');
var fs = require('fs');
var http = require('http');
var url = require('url');
var couchdb = require('felix-couchdb');
var client = couchdb.createClient(5984,'localhost');
var db = client.db('mydb');

http.createServer(function (req, res) {
	var url_parts = url.parse(req.url);
	console.log(url_parts);
	switch(url_parts.pathname){
		case '/':
			display_root(url_parts.pathname,req,res);
			break;
		default:
			display_404(url_parts.pathname,req,res);
			
	}
	return;
	
	function display_root(url,req,res){
		res.writeHead(200,{'Content-Type':'text/html'});
		db.getDoc('golf',function(error,doc){
			if(error){
				res.write("error");
				res.end();
			}else{
		
			res.write("MGB=" + doc.mgb);
			res.end();
			}
		
		});
	
	}
	
	function display_404(url,req,res){
		res.writeHead(404,{'Content-Type':'text/html'});
		res.write("<h1>404 Not Found</h1>");
		res.end("<p>The page you were looking for: " + url + " can not be found");
	}
   

}).listen(3000, "127.0.0.1");